package ssq.utils.wolfmankill;

import net.sf.json.JSONObject;

import org.java_websocket.WebSocket;

public class Participant
{
    WebSocket socket;
    String    name;
    Status    status;
    Role      role;
    int       voteCnt = 0;

    @Override
    public String toString()
    {
        return this.name;
    }

    protected void resetVoteCnt()
    {
        this.voteCnt = 0;
    }

    public Status getStatus()
    {
        return this.status;
    }

    public void setStatus(Status status)
    {
        this.status = status;
    }

    public Role getRole()
    {
        return this.role;
    }

    public void setRole(Role role)
    {
        this.role = role;
    }

    public WebSocket getSocket()
    {
        return this.socket;
    }

    public void setSocket(WebSocket socket)
    {
        this.socket = socket;
    }

    public String getName()
    {
        return this.name;
    }

    String toJson()
    {
        JSONObject result = new JSONObject();

        result.put("name", this.name);
        result.put("status", this.status.toString());
        result.put("role", this.role.toString());

        return result.toString();
    }

    void fromJson(JSONObject json)
    {
        this.name = json.getString("name");
        this.role = Role.valueOf(json.getString("role"));
        this.status = Status.valueOf(json.getString("status"));
    }

    public Participant(WebSocket socket, String name)
    {
        this.socket = socket;
        this.name = name;
    }

    public Participant(String name)
    {
        this.name = name;
    }

    public String toFullString()
    {
        StringBuilder sb = new StringBuilder();
        sb.append("\nname: ").append(this.name).append("\nstatus: ").append(this.status).append("\nrole: ").append(this.role);
        return sb.toString();
    }

    public void forget()
    {
        this.role = Role.outsider;
    }

    public static enum Role
    {
        长老,
        盗贼,
        丘比特,
        狼人,
        预言家,
        守卫,
        女巫,
        平民,
        outsider;

        public boolean isGoodMan()
        {
            return this != 狼人;
        }
    }

    public static enum Status
    {
        alive,
        half_alive,
        dead;
    }
}